package com.neutech.maplestory.util;

import com.neutech.maplestory.constant.Constant;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;

import java.io.BufferedInputStream;
import java.io.FileNotFoundException;

/**
 * 音乐播放的多线程
 * @author Neuedu-远哥
 * @version 1.0
 * @date 2021/12/03 15:04
 */
public class MusicThread extends Thread{
	/**
	 * 播放器类
	 */
	private Player player;
	/**
	 * mp3文件的路径（相对路径）
	 */
	private String musicPath;
	/**
	 * 是否循环
	 */
	private boolean loop;
	public MusicThread() {
		
	}
	/**
	 * 
	 * @param musicName MP3文件的相对路径
	 */
	public MusicThread(String musicName) {
		this();
		this.musicPath = Constant.PRE_MP3 + musicName;
	}
	/**
	 * 
	 * @param musicName MP3文件的相对路径
	 * @param loop 是否循环
	 */
	public MusicThread(String musicName,boolean loop) {
		this(musicName);
		this.loop = loop;
	}
	
	@Override
	public void run() {
		try {
			if(loop) {
				while(true) {
					play();
				}
			}else {
				play();
			}
		} catch (FileNotFoundException | JavaLayerException e) {
			e.printStackTrace();
		}
	}
	private void play() throws FileNotFoundException, JavaLayerException{
		// 创建播放器类
		new Player(new BufferedInputStream(MusicThread.class.getClassLoader().getResourceAsStream(musicPath))).play();
	}
	
	/*public static void main(String[] args) {
		new MusicThread("bgm.mp3", true).start();
	}*/

}